import {Module} from 'vuex'
import {IRootState} from '@/store/types'
import {ISystemState} from './types'
import fileDownload from 'js-file-download';

import {
  getPageListData,
  deletePageData,
  createPageData,
  editPageData,
  DownloadPageListData
} from '@/service/main/system/system'

const systemModule: Module<ISystemState, IRootState> = {
  namespaced: true,
  state() {
    return {
      vwComplaintContentList: [],
      vwComplaintContentCount: 0,
      vwNCVQSContentList: [],
      vwNCVQSContentCount: 0,
      audiComplaintContentList: [],
      audiComplaintContentCount: 0,
      audiNCVQSContentList: [],
      audiNCVQSContentCount: 0,
      Users: [],
      UserCount: 0,
    }
  },
  mutations: {
    changevwComplaintContentList(state, list: any[]) {
      state.vwComplaintContentList = list
    },
    changevwComplaintContentCount(state, count: number) {
      state.vwComplaintContentCount = count
    },
    changevwNCVQSContentList(state, list: any[]) {
      state.vwNCVQSContentList = list
    },
    changevwNCVQSContentCount(state, count: number) {
      state.vwNCVQSContentCount = count
    },
    changeAudiComplaintContentList(state, list: any[]) {
      state.audiComplaintContentList = list
    },
    changeAudiComplaintContentCount(state, count: number) {
      state.audiComplaintContentCount = count
    },
    changeAudiNCVQSContentList(state, list: any[]) {
      state.audiNCVQSContentList = list
    },
    changeAudiNCVQSContentCount(state, count: number) {
      state.audiNCVQSContentCount = count
    },
    changeUsersContentList(state, list: any[]) {
      state.Users = list
    },
    changeUsersContentCount(state, count: number) {
      state.UserCount = count
    },
  },
  getters: {
    pageListData(state) {
      return (pageName: string) => {
        console.log('00. 实时获取当前的数据源，用于界面中的表格', pageName)
        if (pageName === 'vwComplaintContent') {
          return (state as any)['vwComplaintContentList']
        } else if (pageName === 'vwNCVQSContent') {
          return (state as any)['vwNCVQSContentList']
        } else if (pageName === 'audiComplaint') {
          return (state as any)['audiComplaintContentList']
        } else if (pageName === 'audiNCVQS') {
          return (state as any)['audiNCVQSContentList']
        } else if (pageName === 'users') {
          return (state as any)['Users']
        }
      }
    },
    pageListCount(state) {
      return (pageName: string) => {
        if (pageName === 'vwComplaintContent') {
          return (state as any)['vwComplaintContentCount']
        } else if (pageName === 'vwNCVQSContent') {
          return (state as any)['vwNCVQSContentCount']
        } else if (pageName === 'audiComplaint') {
          return (state as any)['audiComplaintContentCount']
        } else if (pageName === 'audiNCVQS') {
          return (state as any)['audiNCVQSContentCount']
        } else if (pageName === 'users') {
          return (state as any)['UserCount']
        }
      }
    }
  },
  actions: {
    // 获取 VW complaint 对应的数据源
    async getvwComplaintPageAction({commit}, payload: any) {
      const pageName = 'vwComplaintContent'
      const pageUrl = `tickets/query/${pageName}/list`
      const pageResult = await getPageListData(pageUrl, payload.queryInfo)
      const {list, totalCount} = pageResult.data
      commit(`changevwComplaintContentList`, list)
      commit(`changevwComplaintContentCount`, totalCount)
    },
    // 下载 VW complaint 对应的数据源
    async downloadvwComplaintPageAction({commit}, payload: any) {
      const pageName = 'vwComplaintContent'
      const pageUrl = `tickets/download/${pageName}/list`
      const res = await DownloadPageListData(pageUrl, payload.queryInfo)
      fileDownload(res.data, "download-vwComplaint.xls")
    },
    // 删除 VW Complaint 当前的这条数据, 这里的删除是统一共享的
    async deleteVWComplaintPageAction({dispatch}, payload: any) {
      const {uuid, sourceId} = payload
      const pageName = 'VWComplaint'
      const pageUrl = `/tickets/delete/${pageName}/${uuid}/${sourceId}`
      await deletePageData(pageUrl)
      dispatch('getvwComplaintPageAction', payload = {
        pageName,
      })
    },
    // 修改 VW Complaint 当前的这条数据
    async editVWComplaintPageDataAction({dispatch}, payload: any) {
      const {editData, uuid} = payload
      const pageName = 'VWComplaint'
      const pageUrl = `/tickets/update/${pageName}/${uuid}`
      await editPageData(pageUrl, editData)
      dispatch('getvwComplaintPageAction', {
        pageName,
      })
    },


    // 获取 VW NCVQS 对应的数据源
    async getvwNCVQSPageAction({commit}, payload: any) {
      const pageName = 'vwNCVQSContent'
      const pageUrl = `tickets/query/${pageName}/list`
      const pageResult = await getPageListData(pageUrl, payload.queryInfo)
      const {list, totalCount} = pageResult.data
      commit(`changevwNCVQSContentList`, list)
      commit(`changevwNCVQSContentCount`, totalCount)
    },
    // 下载 VW NCVQS 对应的数据源
    async downloadvwNCVQSPageAction({commit}, payload: any) {
      const pageName = 'vwNCVQSContent'
      const pageUrl = `tickets/download/${pageName}/list`
      const res = await DownloadPageListData(pageUrl, payload.queryInfo)
      fileDownload(res.data, "download-vwNCVQS.xls")
    },
    // 修改 VW NCVQS 对应的数据源
    async editVWNCVQSPageDataAction({dispatch}, payload: any) {
      const {editData, uuid} = payload
      const pageName = 'VWNCVQS'
      const pageUrl = `/tickets/update/${pageName}/${uuid}`
      await editPageData(pageUrl, editData)
      dispatch('getvwComplaintPageAction', {
        pageName,
      })
    },


    // 获取 Audi Complaint 对应数据源
    async getaudiComplaintPageAction({commit}, payload: any) {
      const pageName = 'audiComplaintContent'
      const pageUrl = `tickets/query/${pageName}/list`
      const pageResult = await getPageListData(pageUrl, payload.queryInfo)
      const {list, totalCount} = pageResult.data
      commit(`changeAudiComplaintContentList`, list)
      commit(`changeAudiComplaintContentCount`, totalCount)
    },
    // 下载 Audi Complaint 对应的数据源
    async downloadAudiComplaintPageAction({commit}, payload: any) {
      const pageName = 'audiComplaintContent'
      const pageUrl = `tickets/download/${pageName}/list`
      const res = await DownloadPageListData(pageUrl, payload.queryInfo)
      fileDownload(res.data, "download-audiComplaint.xls")
    },
    // 编辑 Audi Complaint 当前的数据
    async editAudiComplaintPageDataAction({dispatch}, payload: any) {
      const {editData, uuid} = payload
      const pageName = 'audiComplaint'
      const pageUrl = `/tickets/update/${pageName}/${uuid}`
      await editPageData(pageUrl, editData)
      dispatch('getvwComplaintPageAction', {
        pageName,
      })
    },
    // 删除 Audi 当前的数据
    async deleteAudiComplaintPageAction({dispatch}, payload: any) {
      const {uuid, sourceId} = payload
      const pageName = 'AudiComplaint'
      const pageUrl = `/tickets/delete/${pageName}/${uuid}/${sourceId}`
      await deletePageData(pageUrl)
      dispatch('getaudiComplaintPageAction', payload = {
        pageName,
      })
    },

    // 获取 Audi NCVQS 的数据
    async getaudiNCVQSPageAction({commit}, payload: any) {
      const pageName = 'audiNCVQS'
      const pageUrl = `tickets/query/${pageName}/list`
      const pageResult = await getPageListData(pageUrl, payload.queryInfo)
      const {list, totalCount} = pageResult.data
      commit(`changeAudiNCVQSContentList`, list)
      commit(`changeAudiNCVQSContentCount`, totalCount)
    },
    // 下载 Audi NCVQS 的数据
    async downloadAudiNCVQSPageAction({commit}, payload: any) {
      const pageName = 'audiNCVQSContent'
      const pageUrl = `tickets/download/${pageName}/list`
      const res = await DownloadPageListData(pageUrl, payload.queryInfo)
      fileDownload(res.data, "download-audiNCVQS.xls")
    },
    async editAudiNCVQSPageDataAction({dispatch}, payload: any) {
      const {editData, uuid} = payload
      const pageName = 'audiNCVQS'
      const pageUrl = `/tickets/update/${pageName}/${uuid}`
      await editPageData(pageUrl, editData)
      dispatch('getvwComplaintPageAction', {
        pageName,
      })
    },


    // 获取 用户 相关的数据信息
    async getUserPageAction({commit}, payload: any) {
      const pageName = 'QueryUserList'
      const pageUrl = `/user/query/${pageName}/list`
      const pageResult = await getPageListData(pageUrl, payload.queryInfo)
      const {list, totalCount} = pageResult.data
      commit(`changeUsersContentList`, list)
      commit(`changeUsersContentCount`, totalCount)
    },
    // 更新 或者 新建一个用户
    async editUserSPageDataAction({dispatch}, payload: any) {
      const {editData, user_email} = payload
      const pageName = 'EditUser'
      const pageUrl = `/user/update/${pageName}/${user_email}`
      await editPageData(pageUrl, editData)
      dispatch('getUserPageAction', {
        pageName,
      })
    },

  }
}

export default systemModule
